package com.tenbent.product.base.utils;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 
 * 帮助类获取 SpringContext 获取 注册Bean
 *
 * @author Randy Yan
 *
 * @CreateDate 2010 2010-7-13 下午03:03:04
 *
 */
public class SpringContextUtil implements ApplicationContextAware {

	public static final Logger logger = LoggerFactory.getLogger(SpringContextUtil.class);

	private static ApplicationContext context = null;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.context = applicationContext;
		logger.debug("initialized spring context the spring context util SpringContextUtil");
	}

	public static void setContext(ApplicationContext pcontext) {

		context = pcontext;
	}

	public static ApplicationContext getContext() {

		return context;
	}

	public static Object getBean(String id) {

		return context.getBean(id);
	}

	public static Object getSessionAttribute(String key) {

		Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
		synchronized (mutex) {

			RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
			return ra.getAttribute(key, RequestAttributes.SCOPE_SESSION);
		}
	}

	public static void removeSessionAttribute(String key) {

		Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
		synchronized (mutex) {

			RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
			ra.removeAttribute(key, RequestAttributes.SCOPE_SESSION);
		}
	}

	public static String[] getAllSessionKeys(String key) {

		Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
		synchronized (mutex) {

			RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
			return ra.getAttributeNames(RequestAttributes.SCOPE_SESSION);
		}
	}

	public static void pubSessionAttribute(String key, Object value) {

		Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
		synchronized (mutex) {

			RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
			ra.setAttribute(key, value, RequestAttributes.SCOPE_SESSION);
		}
	}

	public static void pubRequestAttribute(String key, Object value) {

		Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
		synchronized (mutex) {

			RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
			ra.setAttribute(key, value, RequestAttributes.SCOPE_REQUEST);
		}
	}

	public static Object getRequestAttribute(String key) {

		Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
		synchronized (mutex) {

			RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
			return ra.getAttribute(key, RequestAttributes.SCOPE_REQUEST);
		}
	}

	// 获取HttpRequest
	public static HttpServletRequest getRequest() {

		Object ra = RequestContextHolder.currentRequestAttributes();
		if (ra instanceof ServletRequestAttributes) {

			ServletRequestAttributes sra = (ServletRequestAttributes) ra;
			HttpServletRequest hrq = sra.getRequest();
			return hrq;
		}
		return null;
	}

	// 获取getParameters
	public static Map<String, String[]> getParameters() {

		Map<String, String[]> parameterMp = new HashMap<String, String[]>();
		RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
		if (ra instanceof ServletRequestAttributes) {
			ServletRequestAttributes sra = (ServletRequestAttributes) ra;
			HttpServletRequest hrq = sra.getRequest();
			return hrq.getParameterMap();
		}
		return parameterMp;
	}

	// 获取getParameterNames()
	public static Enumeration<String> getParameterNames() {

		RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
		if (ra instanceof ServletRequestAttributes) {
			ServletRequestAttributes sra = (ServletRequestAttributes) ra;
			HttpServletRequest hrq = sra.getRequest();
			return hrq.getParameterNames();
		}
		return null;

	}

	// 获取requestParamter
	public static Object getReqParameter(String key) {

		Map<String, String[]> params = getParameters();
		String[] val = params.get(key);
		if (val == null) {
			return null;
		}
		if (val.length > 1) {
			return val;
		} else if (val.length == 1) {
			return val[0];
		} else {
			return null;
		}
	}

}
